<?php



namespace app\common\logic;

use app\common\library\enum\CodeEnum;
use think\Db;

class Balance extends BaseLogic
{

    /**
     * 获取资产列表
     *
     *
     * @param $where
     * @param $field
     * @param $order
     * @param $paginate
     *
     * @return mixed
     */
    public function getBalanceList($where, $field, $order, $paginate)
    {
        return $this->modelBalance->getList($where, $field, $order, $paginate);
    }


    /**
     * 获取商户资产详情
     *
     *
     * @param array $where
     * @param bool $field
     *
     * @return mixed
     */
    public function getBalanceInfo($where = [], $field = true)
    {
        return $this->modelBalance->getInfo($where, $field);
    }

    /**
     * 获取商户资产列表
     *
     *
     * @param $where
     * @return mixed
     */
    public function getBalanceCount($where = [])
    {
        return $this->modelBalance->getCount($where);
    }

    public function changeEnableMoney($where = [], $value, $autoReduction = false)
    {
        Db::startTrans();
        try {
            $setDec = $value < 0;
            $blance = $this->getBalanceInfo($where);

            if ($autoReduction) {
                $disable = bcsub($blance['disable'], $value, 2);
                if ($disable < 0 ) {
                    Db::rollback();
                    return ['code' => CodeEnum::ERROR, 'msg' => '待结算余额不足'];
                }

                action_log('待结算余额调整', '调整金额：' . - $value);
                $this->logicBalanceChange->creatBalanceChange($blance['uid'], abs($value), '待结算余额调整', 'disable',  !$setDec);
            }

            //资金变动 - 资金记录
            $this->logicBalanceChange->creatBalanceChange($blance['uid'], abs($value), '可用余额调整', 'enable', $setDec);
            Db::commit();
            action_log('可用余额调整', '调整金额：' . $value);
            $blance = $this->getBalanceInfo($where);
            return  ['code' =>  CodeEnum::SUCCESS, 'msg'  => '可用余额调整成功', 'enable' => $blance['enable'], 'disable' => $blance['disable']];
        } catch (\Exception $ex) {
            Log::error("可用余额调整出现错误 : " . $ex->getMessage());
            Db::rollback();
            return ['code' => CodeEnum::ERROR, 'msg' => config('app_debug') ? $ex->getMessage() : '可用余额调整异常'];
        }
    }

    public function changeDisableMoney($where = [], $value)
    {
        Db::startTrans();
        try {
            $setDec=$value < 0;
            $blance = $this->getBalanceInfo($where);
            
            //资金变动 - 资金记录
            $this->logicBalanceChange->creatBalanceChange($blance['uid'], abs($value), '待结算余额调整', 'disable',  $setDec);

            $blance = $this->getBalanceInfo($where);
            $resultVal = bcadd($blance['disable'], 0, 2);

            Db::commit();

            action_log('待结算余额调整', '调整金额：' . $value);

            return  ['code' =>  CodeEnum::SUCCESS, 'msg'  => '待结算余额调整成功', 'data' => $resultVal];
        } catch (\Exception $ex) {
            Log::error("待结算余额调整出现错误 : " . $ex->getMessage());
            Db::rollback();
            return ['code' => CodeEnum::ERROR, 'msg' => config('app_debug') ? $ex->getMessage() : '待结算余额调整异常'];
        }
    }
}
